package com.imooc.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.imooc.redis.RedisService;

@Service
public class MqSender {
	
	private static Logger log = LoggerFactory.getLogger(MqSender.class);

	@Autowired
	AmqpTemplate amqpTemplate;
	
	public void sender(Object message){
		String msg = RedisService.beanToString(message);
		log.info("=======send message=======:" + msg);
		amqpTemplate.convertAndSend(MqConfig.QUEUE, msg);
	}
	
	public void senderTopic(Object message){
		String msg = RedisService.beanToString(message);
		log.info("=======send topic message=======:" + msg);
		amqpTemplate.convertAndSend(MqConfig.TOPIC_EXCHANGE, "topic.key1", msg+"1");
		amqpTemplate.convertAndSend(MqConfig.TOPIC_EXCHANGE, "topic.key2", msg+"2");
	}
	
	public void senderFanout(Object message){
		String msg = RedisService.beanToString(message);
		log.info("=======send fanout message=======:" + msg);
		amqpTemplate.convertAndSend(MqConfig.FANOUT_EXCHANGE, "", msg);
	}
	
	public void senderHeader(Object message){
		String msg = RedisService.beanToString(message);
		log.info("=======send header message=======:" + msg);
		MessageProperties messageProperties = new MessageProperties();
		messageProperties.setHeader("header1", "value1");
		messageProperties.setHeader("header2", "value2");
		
		Message obj = new Message(msg.getBytes(), messageProperties);
		amqpTemplate.convertAndSend(MqConfig.HEADER_EXCHANGE, "", obj);
	}

	// 订单入队(mq)
	public void sendMiaoshaMessage(MiaoshaMessage message) {
		String msg = RedisService.beanToString(message);
		log.info("=======miaosha message=======:" + msg);
		amqpTemplate.convertAndSend(MqConfig.MIAOSHA_QUEUE, msg);
	}
	
}
